package shop.maytinh.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.Transformers;








import shop.maytinh.util.HibernateUtil;
import shop.maytinh.util.KPaging;
import shop.maytinh.model.*;

 

public class ProductManager extends HibernateUtil {
	
	List<Product> listproduct;
	static List<Comment> listcomment;
	public Comment comment = null; 
	private int  total;
	private Integer temp;
	private List<Product> lstfastsell;
	private ArrayList<Product> lstfastsell1 = new ArrayList<Product>();
	private List<Detail_oder> listidproductfastsell;
	private static Product pr;
	
	public ArrayList<Product> getlistidproductfastsell()
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();		
	    session.beginTransaction();	
	    try{
	    	listidproductfastsell = (List<Detail_oder>)session.createQuery("FROM Detail_oder E GROUP BY E.product.id ORDER BY E.quantum DESC").setMaxResults(10).list();
	    	 lstfastsell1 = new ArrayList<Product>();
	    	//private static ArrayList<Room> abode = new ArrayList<Room>();
	    	for(int i=0; i<listidproductfastsell.size(); i++){
	    		temp = listidproductfastsell.get(i).getProduct().getId();
	    		pr = new Product();
	    		pr = TruyVanHinhAnh(temp);
	    		lstfastsell1.add(getPr());
	    	}
	    }	    	
		catch(Exception e){
			 e.printStackTrace();
			 session.getTransaction().rollback();
		}
	    //session.getTransaction().commit();		
		return lstfastsell1;
	}
	public List<Product> getlistproduct(){
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		listproduct = null;
	    session.beginTransaction();			
	    try{
	    	//listproduct = (List<Product>)session.createQuery("from Product WHERE Product.status = "+1).list();	
	    	listproduct = (List<Product>)session.createQuery("from Product E WHERE E.status = '1'").list();
			if(listproduct==null)
				return null;			
		}
		catch(Exception e){
			 e.printStackTrace();
			 session.getTransaction().rollback();
		}
	    session.getTransaction().commit();
		return listproduct;
	}	

public static List<Comment> getCommentByProduct(Integer id) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();			   
        try {
        	session.beginTransaction();
            String hql = "FROM Comment E WHERE E.product.id = :ex";
            Query query = session.createQuery(hql);
            query.setParameter("ex",id);
            query.setMaxResults(1);            
            List employees = query.list();
            //Product pd = new Product();
            listcomment = (List<Comment>)query.list();        
            return listcomment;
        } catch (Exception e) {           
           return null;
        }		
    }
	
	public Product getProductById(Integer id) throws Exception {
		
        try {
        	Session session = HibernateUtil.getSessionFactory().getCurrentSession();    	 
        	session.beginTransaction();
        	Product product = null;                                
        	product = (Product) session.createCriteria(Product.class)
                    .add(Restrictions.eq("id",id))
                    .uniqueResult();
        	session.getTransaction().commit();
            return product;                       
        } catch (Exception e)
        {
            throw e;
        }
        
    }
	public static Product TruyVanHinhAnh(Integer s) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();    	 
    	session.beginTransaction();
        try {
            
            String hql = "FROM Product E WHERE E.id = :ex";
            Query query = session.createQuery(hql);
            query.setParameter("ex",s);
            List employees = query.list();
            pr = new Product();
           
            pr = (Product) employees.toArray()[0];
           
           
        } catch (HibernateException e) {            
            e.printStackTrace();
        } finally {
            session.close();
        }
        return pr;

    }
	public List<Product> SearchProductByPice(Integer s)
	{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		listproduct = null;
	    session.beginTransaction();			
	    try{
	    	listproduct = (List<Product>)session.createQuery("from Product E WHERE E.pice <= "+s).list();	    		    	
			if(listproduct==null)
				return null;			
		}
		catch(Exception e){
			 e.printStackTrace();
			 session.getTransaction().rollback();
		}
	    session.getTransaction().commit();
		return listproduct;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List<Product> getLstfastsell() {
		return lstfastsell;
	}

	public void setLstfastsell(List<Product> lstfastsell) {
		this.lstfastsell = lstfastsell;
	}
	public List<Detail_oder> getListidproductfastsell() {
		return listidproductfastsell;
	}
	public void setListidproductfastsell(List<Detail_oder> listidproductfastsell) {
		this.listidproductfastsell = listidproductfastsell;
	}
	public Integer getTemp() {
		return temp;
	}
	public void setTemp(Integer temp) {
		this.temp = temp;
	}
	public Product getPr() {
		return pr;
	}
	public void setPr(Product pr) {
		this.pr = pr;
	}
	public ArrayList<Product> getLstfastsell1() {
		return lstfastsell1;
	}
	public void setLstfastsell1(ArrayList<Product> lstfastsell1) {
		this.lstfastsell1 = lstfastsell1;
	}
	
	
	
}